package com.example.passwortmanager;

import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class DatabaseContentProvider extends ContentProvider {
	
	private static final String DATABASE_NAME = "passwordmanager.db";
	
	private static final int DATABASE_VERSION = 1;
	
	public static final String AUTHORITY = 
			"com.example.passwortmanager.DatabaseContentProvider";
	
	private static final UriMatcher sUriMatcher;
	
	private static HashMap<String, String> projectionMap;
	
	// URI MATCH OF A GENERAL QUERY
    private static final int CITIZENS = 1;

    // URI MATCH OF A SPECIFIC QUERY
    private static final int SSID = 2;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + CategoryTable.TABLE_NAME + " ("
					+ CategoryTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
					+ CategoryTable.CATECORY_NAME + " TEXT);");
			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
			Log.w("LOG_TAG", "Upgrading database from version " + oldVersion + " to " + newVersion
					+ ", which will destroy all old data");
			
			db.execSQL("DROP TABLE IF EXISTS " + CategoryTable.CATECORY_NAME);
			
			onCreate(db);
			
		}
		
		
    }
    
    private DatabaseHelper dbHelper;
    
	@Override
	public boolean onCreate() {
		dbHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;
		
		switch (sUriMatcher.match(uri)) {
		case CITIZENS:
			count = db.delete(CategoryTable.TABLE_NAME, where, whereArgs);
			break;

		default:
			break;
		}
		return 0;
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		
		if(sUriMatcher.match(uri) != CITIZENS) { throw new IllegalArgumentException("Unknown URI "+ uri); }
		
		ContentValues values;
		if(initialValues != null)
		{
			values = new ContentValues(initialValues);
		}
		else
		{
			values = new ContentValues();
		}
		
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		long rowId = db.insert(CategoryTable.TABLE_NAME, CategoryTable.CATECORY_NAME,values);
		if(rowId > 0)
		{
		}
		return null;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
			String arg4) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, CategoryTable.TABLE_NAME, CITIZENS);
		sUriMatcher.addURI(AUTHORITY, CategoryTable.TABLE_NAME, SSID);
	}

}
